﻿package mortal.game.view.guildNew.member
{
    import Message.Game.*;
    import com.gengine.resource.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import extend.language.*;
    import fl.data.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.cache.guild.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.utils.*;
    import mortal.game.view.chat.chatPrivate.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.pageSelect.*;
    import mortal.game.view.copy.crossMainCity.mainCityPosition.*;
    import mortal.game.view.guild.otherpanel.*;
    import mortal.game.view.guildNew.WareHourse.*;
    import mortal.game.view.guildNew.data.*;
    import mortal.mvc.core.*;

    public class GuildNewMemberPanel extends GSprite implements GuildNewPanelI
    {
        private var _listType:int = 0;
        protected var _bg:ScaleBitmap;
        protected var _titleSp:GuildNewMemTitleSp;
        protected var _memberList:GTileList;
        protected var _btnRecruit:GButton;
        protected var _btnPositonOprate:GButton;
        protected var _btnExitGuild:GButton;
        protected var _pageSelecter:PageSelecter;
        protected var _txtMembersNum:GTextFiled;
        protected var _btnKickOutMember:GButton;
        protected var _btnSetCondition:GButton;
        protected var _btnDisbandGuild:GButton;
        protected var _outgoingBtn:GButton;
        protected var _sendGuildMail:GButton;
        protected var _sendGuildMg:GButton;
        private var _txtWarnMember:GTextFiled;
        protected var _bmpWarnNumBg:GBitmap;
        protected var _txtWarnMemberNum:GTextFiled;
        protected var _txtApply:GTextFiled;
        protected var _applyNum:GuildNewWareHAppNum;
        private var _sortFunction:Function;
        private static const PAGE_MEMBER_COUNT:int = 11;

        public function GuildNewMemberPanel()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._bg = UIFactory.bg(0, 0, 745, 475, this, ImagesConst.PanelBg);
            this._memberList = UIFactory.tileList(-1, 24, 760, 395, this);
            this._memberList.verticalGap = 1;
            this._memberList.horizontalGap = 2;
            this._memberList.rowHeight = 34;
            this._memberList.columnWidth = 745;
            this._memberList.columnCount = 1;
            this._memberList.width = 760;
            this._memberList.setStyle("cellRenderer", GuildNewMemberCell);
            this._btnRecruit = UIFactory.gButton("" + Language.getString(71505), 553, 413, 70, 26, this);
            this._btnPositonOprate = UIFactory.gButton("" + Language.getString(71506), 633, 413, 70, 26, this);
            this._btnExitGuild = UIFactory.gButton("" + Language.getString(71507), 36, 442, 70, 26, this);
            this._txtMembersNum = UIFactory.gTextField("", 304, 443, 120, 20, this, GlobalStyle.textFormatPutong.setColor(16777215));
            this._btnKickOutMember = UIFactory.gButton("" + Language.getString(71508), 116, 413, 70, 26, this);
            this._btnSetCondition = UIFactory.gButton("" + Language.getString(71509), 422, 439, 70, 26, this);
            this._btnDisbandGuild = UIFactory.gButton("" + Language.getString(71510), 116, 442, 70, 26, this);
            this._outgoingBtn = UIFactory.gButton("" + Language.getString(71511), 36, 413, 70, 26, this);
            this._sendGuildMail = UIFactory.gButton("" + Language.getString(71512), 553, 443, 70, 26, this);
            this._sendGuildMg = UIFactory.gButton("" + Language.getString(71698), 633, 443, 70, 26, this);
            this._bmpWarnNumBg = UIFactory.gBitmap(ImagesConst.NumberBg, 277, 417);
            this._txtWarnMemberNum = UIFactory.gTextField("99", 277, 417, 100, 20, this, GlobalStyle.textFormatPutong.setColor(16777215));
            this._applyNum = UIFactory.getUICompoment(GuildNewWareHAppNum, 276, 443, this);
            var _loc_1:* = "<a href=\'event:goodMemberLink\' target = \'\'><font color=\'#00ff00\'><u>" + Language.getString(60039) + "</u></font></a>";
            this._txtWarnMember = UIFactory.gTextField(_loc_1, 199, 416, 100, 22, this, null, true);
            this._txtWarnMember.configEventListener(TextEvent.LINK, this.onTextLink);
            var _loc_2:* = "<a href=\'event:applyMemberLink\' target = \'\'><font color=\'#FAD200\'><u>" + Language.getString(71514) + "</u></font></a>";
            this._txtApply = UIFactory.gTextField(_loc_2, 200, 445, 100, 22, this, null, true);
            this._txtApply.configEventListener(TextEvent.LINK, this.onTextLinkApply);
            this._titleSp = UIFactory.getUICompoment(GuildNewMemTitleSp, 0, 0, this);
            var _loc_3:* = GlobalStyle.textFormatBai;
            _loc_3.align = "center";
            this._pageSelecter = UIFactory.pageSelecter(323, 413, this, PageSelecter.CompleteMode);
            this._pageSelecter.setbgStlye(ImagesConst.ComboBg, _loc_3);
            this._pageSelecter.maxPage = 3;
            this._pageSelecter.currentPage = 1;
            this._pageSelecter.pageTextBoxSize = 45;
            this._pageSelecter.configEventListener(Event.CHANGE, this.onPageChange);
            var _loc_4:* = GuildConst.hasPermissions(GuildConst.OpenWarnListPanel);
            this._txtWarnMemberNum.visible = GuildConst.hasPermissions(GuildConst.OpenWarnListPanel);
            var _loc_4:* = _loc_4;
            this._bmpWarnNumBg.visible = _loc_4;
            this._txtWarnMember.visible = _loc_4;
            this._txtWarnMember.configEventListener(TextEvent.LINK, this.onTextLink);
            this._txtApply.visible = GuildConst.hasPermissions(GuildConst.ApproveIntoGuild);
            this._applyNum.updateNum(0);
            this._btnSetCondition.visible = GuildConst.hasPermissions(GuildConst.SetApplyCondition);
            var _loc_4:* = GuildConst.hasPermissions(GuildConst.SendGuildMail);
            this._sendGuildMg.visible = GuildConst.hasPermissions(GuildConst.SendGuildMail);
            this._sendGuildMail.visible = _loc_4;
            this._sendGuildMail.configEventListener(MouseEvent.CLICK, this.onSendMailCk);
            this._sendGuildMg.configEventListener(MouseEvent.CLICK, this.onSendChatCk);
            this._txtMembersNum.x = this._btnSetCondition.visible ? (304) : (348);
            this._sortFunction = GuildUtil.defaultMemberListSortFunc;
            this.configEventListener(MouseEvent.CLICK, this.onMouseClick);
            LoaderHelp.addResCallBack(ResFileConst.guildOtherPanel, this.onResLoad);
            return;
        }// end function

        private function onResLoad() : void
        {
            this.update();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            LoaderHelp.removeResEvent(ResFileConst.guildOtherPanel, this.onResLoad);
            this._sortFunction = null;
            this._titleSp = null;
            this._bg = null;
            this._memberList = null;
            this._btnRecruit = null;
            this._btnPositonOprate = null;
            this._btnExitGuild = null;
            this._pageSelecter = null;
            this._txtMembersNum = null;
            this._btnKickOutMember = null;
            this._btnSetCondition = null;
            this._btnDisbandGuild = null;
            this._outgoingBtn = null;
            this._sendGuildMail = null;
            this._sendGuildMg = null;
            this._txtWarnMember = null;
            this._bmpWarnNumBg = null;
            this._txtWarnMemberNum = null;
            this._txtApply = null;
            this._applyNum = null;
            if (MainCityAppointSmallWin.isViewShow)
            {
                MainCityAppointSmallWin.instance.hide();
            }
            return;
        }// end function

        private function onSendMailCk(event:MouseEvent) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(GuildNewLocalEvent.GuildNewMemberSendMail));
            return;
        }// end function

        private function onSendChatCk(event:MouseEvent) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(GuildNewLocalEvent.GuildNewMemberChat));
            return;
        }// end function

        public function set listType(param1:int) : void
        {
            this._listType = param1;
            if (this._listType == 0)
            {
                this._titleSp._btnSortByOnLine.visible = true;
                this._memberList.setStyle("cellRenderer", GuildNewMemberCell);
                this.reqData();
            }
            else if (this._listType == 1)
            {
                this._titleSp._btnSortByOnLine.visible = false;
                this._titleSp._txtSate.text = "" + Language.getString(71515);
            }
            return;
        }// end function

        private function reqData() : void
        {
            if (this._listType == 0)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.Guild_memberList_get));
                if (GuildConst.hasPermissions(GuildConst.OpenWarnListPanel))
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.Guild_warnMemberList_get));
                }
            }
            return;
        }// end function

        private function onTextLink(event:TextEvent) : void
        {
            GuildWarnMembersPanel.instance.popUp();
            return;
        }// end function

        private function onTextLinkApply(event:TextEvent) : void
        {
            var _loc_2:* = Cache.instance.guild.selfGuildInfo;
            if (_loc_2 == null)
            {
                return;
            }
            if (_loc_2.applyNum == 0)
            {
                MsgManager.showRollTipsMsg("" + Language.getString(71516));
                return;
            }
            GuildApplyListPanel.instance.popUp();
            Dispatcher.dispatchEvent(new DataEvent(EventName.GUILD_APPLY_LIST_GET, null));
            return;
        }// end function

        public function update() : void
        {
            var _loc_1:SelfGuildInfo = null;
            var _loc_5:int = 0;
            var _loc_6:SGuildMember = null;
            if (this._listType == 0)
            {
                _loc_1 = Cache.instance.guild.selfGuildInfo;
            }
            else if (this._listType == 1)
            {
                _loc_1 = Cache.instance.guild.otherGuildInfoV2;
            }
            this._sortFunction = this._titleSp.sortFunction;
            if (this._sortFunction == null)
            {
                this._sortFunction = GuildUtil.defaultMemberListSortFunc;
            }
            _loc_1.memberList.sort(this._sortFunction);
            this._pageSelecter.maxPage = Math.ceil(_loc_1.memberList.length / PAGE_MEMBER_COUNT);
            var _loc_2:* = (this._pageSelecter.currentPage - 1) * PAGE_MEMBER_COUNT;
            var _loc_3:* = new DataProvider();
            var _loc_4:int = 0;
            while (_loc_4 < PAGE_MEMBER_COUNT)
            {
                
                _loc_5 = _loc_2 + _loc_4;
                if (_loc_5 >= _loc_1.memberList.length)
                {
                    break;
                }
                _loc_6 = _loc_1.memberList[_loc_5];
                _loc_3.addItem(_loc_6);
                _loc_4++;
            }
            this._memberList.dataProvider = _loc_3;
            this._memberList.drawNow();
            if (this._listType == 0)
            {
                this._txtMembersNum.text = Language.getString(60040) + ":" + _loc_1.memberList.length + "/" + _loc_1.baseInfo.maxPlayerNum;
                this._txtWarnMemberNum.text = _loc_1.warnMemberNum.toString();
                this._outgoingBtn.label = GuildConst.hasPermissions(GuildConst.TransferLeader) ? ("" + Language.getString(71517)) : (Language.getString(71518));
                if (GuildConst.hasPermissions(GuildConst.OpenWarnListPanel))
                {
                    var _loc_7:* = Boolean(_loc_1.warnMemberNum > 0);
                    this._txtWarnMemberNum.visible = Boolean(_loc_1.warnMemberNum > 0);
                    this._bmpWarnNumBg.visible = _loc_7;
                }
                this._btnPositonOprate.visible = GuildConst.hasPermissions(GuildConst.ManagerMembers);
                this._btnDisbandGuild.visible = GuildConst.hasPermissions(GuildConst.DisbandGuild);
                this._btnRecruit.visible = GuildConst.hasPermissions(GuildConst.Recruit);
                this._btnKickOutMember.visible = GuildConst.hasPermissions(GuildConst.KickOutMember);
                this._outgoingBtn.visible = GuildConst.hasPermissions(GuildConst.OutgoingPositon) || GuildConst.hasPermissions(GuildConst.TransferLeader);
            }
            else if (this._listType == 1)
            {
                this._txtMembersNum.text = Language.getString(60040) + ":" + _loc_1.memberList.length;
                this._txtWarnMemberNum.text = "";
                this._bmpWarnNumBg.visible = false;
                this._btnPositonOprate.visible = false;
                this._btnDisbandGuild.visible = false;
                this._btnRecruit.visible = false;
                this._btnKickOutMember.visible = false;
                this._outgoingBtn.visible = false;
                this._btnSetCondition.visible = false;
                this._txtWarnMember.visible = false;
                this._bmpWarnNumBg.visible = false;
                this._btnExitGuild.visible = false;
            }
            this.updateApply();
            return;
        }// end function

        private function updateApply() : void
        {
            var _loc_1:* = Cache.instance.guild.selfGuildInfo.applyNum;
            this._txtApply.visible = GuildConst.hasPermissions(GuildConst.ApproveIntoGuild);
            if (GuildConst.hasPermissions(GuildConst.ApproveIntoGuild))
            {
                this._applyNum.updateNum(Cache.instance.guild.selfGuildInfo.applyNum);
            }
            return;
        }// end function

        public function onMouseClick(event:MouseEvent) : void
        {
            var memberInfo:SGuildMember;
            var onOutgoing:Function;
            var obj:Object;
            var playerName:String;
            var posName:String;
            var event:* = event;
            onOutgoing = function (param1:int) : void
            {
                if (param1 == Alert.OK)
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GUILD_POSITION_OPRATE, obj));
                }
                return;
            }// end function
            ;
            switch(event.target)
            {
                case this._btnRecruit:
                {
                    if (Cache.instance.role.entityInfo.combat < 30000)
                    {
                        MsgManager.showRollTipsMsg("" + Language.getString(71519));
                        return;
                    }
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GUILD_RECRUIT, null));
                    break;
                }
                case this._btnExitGuild:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GUILD_EXIT, null));
                    break;
                }
                case this._btnDisbandGuild:
                {
                    if (Cache.instance.guild.selfGuildInfo.baseInfo && Cache.instance.guild.selfGuildInfo.baseInfo.level >= 6)
                    {
                        MsgManager.showRollTipsMsg("" + Language.getString(71520));
                        return;
                    }
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GUILD_DISBAND, null));
                    break;
                }
                case this._btnPositonOprate:
                {
                    if (this._memberList.selectedIndex == -1)
                    {
                        MsgManager.showRollTipsMsg("" + Language.getString(71521));
                        return;
                    }
                    memberInfo = this._memberList.dataProvider.getItemAt(this._memberList.selectedIndex) as SGuildMember;
                    GuildAppointPanel.instance.setMemberData(memberInfo);
                    GuildAppointPanel.instance.popUp();
                    break;
                }
                case this._btnKickOutMember:
                {
                    if (this._memberList.selectedIndex == -1)
                    {
                        MsgManager.showRollTipsMsg("" + Language.getString(71521));
                        return;
                    }
                    memberInfo = this._memberList.dataProvider.getItemAt(this._memberList.selectedIndex) as SGuildMember;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GUILD_KICK_OUT_MEMEBER, memberInfo.miniPlayer.entityId.id));
                    break;
                }
                case this._btnSetCondition:
                {
                    GuildApplyConditionPanel.instance.popUp();
                    break;
                }
                case this._outgoingBtn:
                {
                    if (this._memberList.selectedIndex == -1)
                    {
                        MsgManager.showRollTipsMsg("" + Language.getString(71521));
                        return;
                    }
                    obj = new Object();
                    if (GuildConst.hasPermissions(GuildConst.TransferLeader))
                    {
                        memberInfo = this._memberList.dataProvider.getItemAt(this._memberList.selectedIndex) as SGuildMember;
                        if (memberInfo.position >= EGuildPosition._EGuildDeputyLeader)
                        {
                            obj.targetID = memberInfo.miniPlayer.entityId.id;
                            obj.position = EGuildPosition._EGuildLeader;
                            playerName = memberInfo.miniPlayer.name;
                            Alert.show(Language.getStringByParam(70487, playerName), null, Alert.OK | Alert.CANCEL, null, onOutgoing);
                        }
                        else
                        {
                            MsgManager.showRollTipsMsg("" + Language.getString(71524));
                            return;
                        }
                    }
                    else
                    {
                        obj.targetID = Cache.instance.role.entityInfo.entityId.id;
                        obj.position = EGuildPosition._EGuildMember;
                        posName = GuildUtil.getPosName(Cache.instance.guild.selfGuildInfo.selfInfo.position);
                        Alert.show(Language.getStringByParam(70486, posName), null, Alert.OK | Alert.CANCEL, null, onOutgoing);
                    }
                    break;
                }
                case this._sendGuildMg:
                {
                    ChatManager.addGuildWindow();
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function onPageChange(event:Event) : void
        {
            this.update();
            return;
        }// end function

    }
}
